package com.three.aboutus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.aboutus.feign.UserFeign;
import com.three.aboutus.service.AboutService;
import com.three.core.entity.About;
import com.three.core.entity.User;
import com.three.core.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.three.aboutus.dao.AboutMapper;

import java.util.Date;

/**
 * @FileName: AboutServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/14 14:27
 * @Version: 1.0.0
 */
@Service
public class AboutServiceImpl implements AboutService {
    @Autowired
    private AboutMapper aboutMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R<Page<About>>  findAboutByCondition(Integer currentPage, Integer pageSize) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        Page<About> page = new Page<>(currentPage, pageSize);
        Page<About> records = aboutMapper.findAboutByCondition(page, userId);
        return new R<>(200, "查询成功", records);
    }

    @Override
    public R deleteAboutById(Integer id) {
        int i = aboutMapper.deleteById(id);
        if (i > 0) {
            return new R(200, "删除成功", null);
        } else {
            return new R(500, "删除失败", null);
        }
    }

    @Override
    public R<About> insertAbout(About about) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        about.setCreateBy(userId);
        about.setIsvalid("0");
        about.setCreateTime(new Date());
        int insert = aboutMapper.insert(about);
        if (insert > 0) {
            return new R(200, "添加成功", null);
        } else {
            return new R(500, "添加失败", null);
        }
    }

    @Override
    public R updateAbout(About about) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        about.setUpdateTime(new Date());
        about.setUpdateBy(userId);
        int i = aboutMapper.update(about, null);
        if (i > 0) {
            return new R(200, "修改成功", null);
        } else {
            return new R(500, "修改失败", null);
        }
    }

    @Override
    public R<About> updateIsValid(About about) {
        int i = aboutMapper.updateIsvalid(about);
        if (i > 0) {
            return new R(200, "修改成功", null);
        } else {
            return new R(500, "修改失败", null);
        }
    }


    @Override
    public R<About> selectByName(String name) {
        About about = aboutMapper.selectByName(name);
        if (about != null) {
            return new R(200, "查询成功", about);
        } else {
            return new R(500, "查询失败", null);
        }
    }

    @Override
    public R<About> selectFirst() {
        About about = aboutMapper.selectFirst();
        if (about != null) {
            return new R(200, "查询成功", about);
        } else {
            return new R(500, "查询失败", null);
        }
    }


}
